<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)*  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Twitter Class
 * File: twitter_class.php
 * Purpose: Obtaining twitter information
 * Todo: Add more advanced functionality
 *
 */

class Twitter {
	private $name;
	
	/**
	* Used to set a twitter name request
	*
	* @param string $name
	* @access public
	*/	
	public function setName($name) {
		// set the name
		$this->name = $name;
	}
	
	/**
	* Used to fetch web files
	*
	* @return string or boolean on fail
	* @access public
	*/	
	public function getTwitterInfo() {
		// create the URL
		$url = 'http://twitter.com/users/show.xml?screen_name=' . $this->name;
		
		// fetch contents
		$contents = KernelBaseSystem::fetchWebFile($url);
		
		// was the request successful?
		if($contents === false) {
			// didn't work, twitter problem perhaps?
			return false;
		} else {
			// did work and has contents
			// attempt to parse contents
			try {
				$xml = new SimpleXMLElement($contents);
			} catch (Exception $e) {
				// unable to parse contents
				trigger_error($e, E_USER_WARNING);
				$failed = true;
			}
			
			// did twitter send back an xml error?
			switch($failed) {
				case true:
					// xml error
					return false;
					
					// end
					break;
				default:
					// was successful
					if(isset($xml->error)) {
						// twitter returned error code
						return false;
					} else {
						// no errors
						if(isset($xml->status->text)) {
							// does have a status, build an array
							$twit_array = array('last_status' => (string)$xml->status->text,
												'profile_image' => (string)$xml->profile_image_url,
												'profile_background_image' => (string)$xml->profile_background_image_url,
												'followers' => (string)$xml->followers_count,
												'screen_name' => (string)$xml->screen_name);
							
							// return this
							return $twit_array;
						} else {
							// has no updates, render pointless
							return false;
						}
					}
					
					// end
					break;
			}
		}
	}
}

?>